<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="技师名称">
            <a-input v-decorator="['name']" placeholder="请输入技师名称" />
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
        </a-form>
      </a-row>

      <a-button type="primary" icon="plus" @click="handleAdd">新增</a-button>
    </div>
    <s-table
      ref="table"
      rowKey="id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
      :locale="{emptyText: '暂无数据'}"
    >
      <span slot="image" slot-scope="text">
        <a title="点击查看原图" :href="text" target="_blank">
          <img width="50" height="50" :src="text" alt="图片" />
        </a>
      </span>
      <span slot="action" slot-scope="text, item">
        <a style="margin-right: 8px;" @click="handleEdit(item)">编辑</a>
        <a @click="handleDelete(item)">删除</a>
      </span>
    </s-table>
    <AddForm ref="AddForm" :addressDataTree="addressDataTree" @handleSubmit="handleRefresh" />
    <EditForm ref="EditForm" :addressDataTree="addressDataTree" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Store from '@/api/store'
import * as Api from '@/api/goods/proficient'
import { STable } from '@/components'
import AddForm from './modules/AddForm'
import EditForm from './modules/EditForm'

export default {
  name: 'Index',
  components: {
    STable,
    AddForm,
    EditForm
  },
  data () {
    return {
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      addressDataTree: [],
      // 列表
      list: [],
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      isAdd: false,
      // 表头
      columns: [
        {
          title: '技师ID',
          dataIndex: 'id'
        },
        {
          title: '技师图片',
          dataIndex: 'image',
          scopedSlots: { customRender: 'image' }
        },
        {
          title: '技师名称',
          dataIndex: 'name'
        },
        {
          title: '所属门店',
          dataIndex: 'addressName'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(result => {
            this.addressDataTree = result.data.address
            return result.data.list
          })
      }
    }
  },
  created () {
    this.storeInfo()
  },
  methods: {
    storeInfo() {
      Store.info().then((result) => {
        const { data: { storeInfo } } = result
        this.isAdd = storeInfo.isAdd
      })
    },

    /**
     * 获取分类列表
     */
    getList (bool) {
      bool && (this.isLoading = true)
      Api.list()
        .then(result => {
          this.list = result.data.list
        })
        .finally(result => {
          this.isLoading = false
        })
    },

    /**
     * 新增记录
     */
    handleAdd () {
      this.$refs.AddForm.add(this.isAdd)
    },

    /**
     * 编辑记录
     */
    handleEdit (item) {
      this.$refs.EditForm.edit(item, this.isAdd)
    },

    /**
     * 删除记录
     */
    handleDelete (item) {
      const self = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk () {
          return Api.deleted({ id: item['id'] })
            .then((result) => {
              self.$message.success(result.message, 1.5)
              self.handleRefresh()
            })
            .finally(result => {
              modal.destroy()
            })
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      // 获取分类列表
      this.$refs.table.refresh(bool)
    },

    // 确认搜索
    handleSearch (e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    }
  }
}
</script>
